package com.somiz.affectation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import org.hibernate.Session;

import com.gest.util.PdfUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.somiz.main.Control;
import com.somiz.main.Principal;
import com.tech.hibernate.HibernateUtil;

public class AffectationPanListe extends JPanel {

	private ModelListeAffectation modelListeAffectation = new ModelListeAffectation();
	private List<Affectation> affectations = new ArrayList<Affectation>();
	private JTable tableau;

	public AffectationPanListe() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.close();

		setLayout(null);
		setLayout(null);
		tableau = new JTable(modelListeAffectation);
		tableau.setRowHeight(25);
		JScrollPane scrollPane = new JScrollPane(tableau);
		scrollPane.setBounds(0, 72, 913, 446);
		this.add(scrollPane);
		JButton button = new JButton("");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Session session = HibernateUtil.getSessionFactory()
						.openSession();
				Control.getAffectationForm().vider(session);
				Control.getPrincipal()
						.getTabbedPane()
						.addTab("Affectations",
								new ImageIcon(
										Principal.class
												.getResource("/cc/black/png/cert_icon&16.png")),
								Control.getAffectationForm(), null);
				Control.getPrincipal()
						.getTabbedPane()
						.setSelectedIndex(
								Control.getPrincipal().getTabbedPane()
										.getTabCount() - 1);
				session.close();
			}
		});
		button.setIcon(new ImageIcon(AffectationPanListe.class
				.getResource("/images/png/Add32.png")));
		button.setPreferredSize(new Dimension(50, 50));
		button.setMnemonic(KeyEvent.VK_ENTER);
		button.setBackground(Color.WHITE);
		button.setBounds(10, 11, 50, 50);
		add(button);

		JButton button_1 = new JButton("");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Session session = HibernateUtil.getSessionFactory()
						.openSession();
				actualisationFiltre(session);
				session.close();
			}
		});
		button_1.setIcon(new ImageIcon(AffectationPanListe.class
				.getResource("/cc/black/png/reload_icon&32.png")));
		button_1.setPreferredSize(new Dimension(50, 50));
		button_1.setMnemonic(KeyEvent.VK_ENTER);
		button_1.setBackground(Color.WHITE);
		button_1.setBounds(70, 11, 50, 50);
		add(button_1);

		JButton button_2 = new JButton("");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Session session = HibernateUtil.getSessionFactory()
						.openSession();
				creerPdfStructure(session, "c:/temp/listeStructure.pdf");
				PdfUtil.OuvrirePdf("c:/temp/listeStructure.pdf", "Structures");
				session.close();
			}
		});
		button_2.setIcon(new ImageIcon(AffectationPanListe.class
				.getResource("/cc/black/png/doc_lines_stright_icon&32.png")));
		button_2.setPreferredSize(new Dimension(50, 50));
		button_2.setMnemonic(KeyEvent.VK_ENTER);
		button_2.setBackground(Color.WHITE);
		button_2.setBounds(132, 11, 50, 50);
		add(button_2);

		session = HibernateUtil.getSessionFactory().openSession();
		actualisationFiltre(session);
		session.close();
	}

	public void creerPdfStructure(Session session, String lien) {
		String FILE = lien;
		Font catFont = new Font(Font.SERIF, 18, Font.BOLD);
		PdfPTable table = new PdfPTable(3);
		try {
			Document document = new Document();
			PdfWriter.getInstance(document, new FileOutputStream(FILE));
			document.open();
			document.addTitle("Liste des affectations");
			Paragraph preface = new Paragraph();
			PdfUtil.addEmptyLine(preface, 2);
			document.add(preface);
			List<Affectation> affectations = new ArrayList<Affectation>(0);
			affectations = Control.getAffectationDao().charger(
					new Affectation(), session);
			table = modelListeAffectation.CreerHeaderPdf(table);
			for (int i = 0; i < affectations.size(); i++) {
				table = modelListeAffectation.addObjectPDF(affectations.get(i),
						table);
			}
			document.add(table);
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void actualisationFiltre(Session session) {
		modelListeAffectation.removeAllObjects();
		List<Affectation> affectations = new ArrayList<Affectation>(0);
		Affectation affectation = new Affectation();
		affectations = Control.getAffectationDao()
				.charger(affectation, session);
		for (int i = 0; i < affectations.size(); i++) {
			modelListeAffectation.addObject(affectations.get(i));
		}
	}
}
